use crate::error::Error;
use syn::Result;
use syn::{Meta, MetaList, MetaNameValue, Path};

pub trait FromMeta: Sized {
    fn from_meta(item: &Meta) -> Result<Self> {
        match item {
            Meta::Path(value) => Self::from_path(value),
            Meta::List(value) => Self::from_list(value),
            Meta::NameValue(value) => Self::from_name_value(value),
        }
    }

    #[allow(unused_variables)]
    fn from_path(path: &Path) -> Result<Self> {
        Err(Error::unsupported_format("path").into())
    }

    #[allow(unused_variables)]
    fn from_list(list: &MetaList) -> Result<Self> {
        Err(Error::unsupported_format("list").into())
    }

    #[allow(unused_variables)]
    fn from_name_value(name_value: &MetaNameValue) -> Result<Self> {
        Err(Error::unsupported_format("name_value").into())
    }
}

impl FromMeta for Path {
    fn from_meta(item: &Meta) -> Result<Self> {
        match item {
            Meta::Path(value) => Ok(value.clone()),
            Meta::List(value) => Self::from_list(value),
            Meta::NameValue(value) => Self::from_name_value(value),
        }
    }
}
